package com.food.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.food.entity.vo.IndexVo;
import com.food.mapper.FoodMapper;
import com.food.service.IndexService;
import com.food.entity.pojo.Food;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class IndexServiceImpl implements IndexService {

    @Autowired
    private FoodMapper foodMapper;

    @Override
    public IndexVo getIndexData() {
        IndexVo vo = new IndexVo();
        vo.setMainImage("/images/homepage-main.jpg"); // 主图地址
        vo.setFoodList(getFoodList()); // 获取美食列表
        return vo;
    }

    /**
     * 查询所有美食并转换为 VO 列表
     */
    private List<IndexVo.FoodVo> getFoodList() {
        // 查询所有美食
        List<Food> foods = foodMapper.selectList(new QueryWrapper<>());

        // 转换为 VO 列表
        return foods.stream().map(food -> {
            IndexVo.FoodVo foodVo = new IndexVo.FoodVo();
            foodVo.setFoodId(food.getFoodId());
            foodVo.setName(food.getName());
            foodVo.setDescription(food.getDescription());
            foodVo.setPrice(food.getPrice());
            foodVo.setImageUrl(food.getImageUrl());
            foodVo.setCategory(food.getCategory());
            return foodVo;
        }).collect(Collectors.toList());
    }
}
